package com.starchen.optimalselection.product.controller;

import com.starchen.optimalselection.common.model.ResultPage;
import com.starchen.optimalselection.common.utils.Result;
import com.starchen.optimalselection.common.model.AttributeValue;
import com.starchen.optimalselection.common.model.form.AttributeForm;
import com.starchen.optimalselection.common.model.vo.AttrQueryVo;
import com.starchen.optimalselection.common.model.vo.AttrRespVo;
import com.starchen.optimalselection.common.model.vo.AttributeVo;
import com.starchen.optimalselection.common.service.AttributeService;
import com.starchen.optimalselection.common.service.AttributeValueService;
import com.starchen.optimalselection.common.model.Attribute;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


/**
 * 商品属性
 *
 * @author daxilong
 * @email daxilong@gmail.com
 * @date 2022-08-14 19:57:36
 */
@Api(tags = "规格属性管理")
@RestController
@RequestMapping("/product/attr")
public class AttributeController {

    @Autowired
    private AttributeService attrService;

    @Autowired
    private AttributeValueService AttributeValueService;

    @ApiOperation("分页查询规格属性")
    @GetMapping("/page")
    public Result baseAttrPage(AttributeForm attrQueryVo){
        ResultPage page = attrService.queryPage(attrQueryVo);
        return Result.success(page);
    }

    /**
     * 根据组id查询所有属性
     * @param groupId
     * @return
     */
    @ApiOperation("根据分组ID查询所有规格属性")
    @GetMapping("/list")
    public Result baseAttrList(Long groupId){
        List<Attribute> attrList = attrService.getListByGroup(groupId);
        return Result.success(attrList);
    }

    @GetMapping("/listbyspu/{spuId}")
    public Result baseAttrlistforspu(@PathVariable("spuId") Long spuId){

        List<AttributeValue> entities = AttributeValueService.baseAttrlistforspu(spuId);

        return Result.success(entities);
    }

    @PostMapping("/updatespuattr/{spuId}")
    public Result updateSpuAttr(@PathVariable("spuId") Long spuId, @RequestBody List<AttributeValue> entities){
        AttributeValueService.updateSpuAttr(spuId,entities);
        return Result.success();
    }

    /**
     * 根据分类id查询所有属性
     * @param attrQueryVo
     * @return
     */
    @ApiOperation("根据分类ID查询所有规格属性")
    @GetMapping("/listbycate")
    public Result baseAttrList(AttrQueryVo attrQueryVo){
        List<Attribute> attrList = attrService.queryList(attrQueryVo);
        return Result.success(attrList);
    }

    @ApiOperation("根据ID查询规格属性")
    @GetMapping("/info")
    public Result info(Long id){
        AttrRespVo respVo = attrService.getAttr(id);
        return Result.success(respVo);
    }

    @ApiOperation("新建规格属性")
    @PostMapping("/create")
    public Result save(@RequestBody AttributeVo attr){
        attrService.saveAttribute(attr);
        return Result.success();
    }

    @ApiOperation("修改规格属性")
    @PostMapping("/update")
    public Result update(@RequestBody AttributeVo attr){
        attrService.updateAttr(attr);
        return Result.success();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody Long[] ids){
        attrService.removeByIds(Arrays.asList(ids));
        return Result.success();
    }


}
